<?php

/**
 * @author MadPitbull
 * @copyright 2011
 * 
 * pass = @#madirc_bot#@ para Master-Mind registrado con el mail alex@madhacking.com
 * 
 */
	set_time_limit(0); //para que la ejecucion no se acabe
	ini_set('display_errors', 'on'); //se muestran todos los errores
	
	$config = array(
		'server' => 'irc.freenode.net',
		'port' =>'6667',
		'channel' => '#madhacking',
        'name'   => 'Master-Mind', 
        'nick'   => 'Master-Mind [PHP]', 
        'pass'   => '@#madirc_bot#@'
	);
	
	
	class MasterMind {
		
		var $socketHolder;
		var $messages = array() ;
		
		

		/**
		 * MasterMind::__construct()
		 * Conects to the irc server and logs in with the $config['nick']
		 * @param mixed $config array
		 * @return
		 */
		function __construct($config) {
			$this->socketHolder = fsockopen($config['server'],$config['port']);
			$this->joinToChannel($config['channel']);
		}
		
		function mainController($config){
			$socketData = fgets($this->socketHolder, 4096);
			flush();
			
			$this->socketHolder = explode(' ',$socketData);
			
			if ($this->socketHolder[0] == 'PING'){
				$this->sendSocketData('PING', $socketData[1]);
			}
			
			$command = str_replace(array(chr(10), chr(13)), '', $this->socketHolder[3]);
			
			$this->mainController($config);
		}
		
		function logToServer($config) {
			$this->sendSocketData('USER', $config['nick'].' madhacking.com '.$config['nick'].' :'.$config['name']);
		}
		
		
		
		
		
		function sendSocketData($command, $message = null) {
			if ($message == null) {
				fputs ($this->socketHolder, $command . "\r\n");
				echo $command."\r\n";
			} else {
				fputs($this->socketHolder, $command." ".$message."\r\n");
				echo $command." ".$message."\r\n";
			}
		}
		
		
		
		function joinToChannel($channel) {
			if(is_array($channel)) {
				foreach ($channel as $channels) {
					$this->sendSocketData('JOIN', $channels);
				}
			}else{
				$this->sendSocketData('JOIN', $channel);
			}
		}
		
	}

?>